package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.product.domain.Category;
import com.spzx.product.mapper.CategoryMapper;
import com.spzx.product.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.product.vo.CategoryVo;
import io.jsonwebtoken.lang.Collections;
import org.apache.poi.ss.usermodel.CellStyle;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品分类 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2025-04-28
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Override
    public List<Category> treeSelect(Long parentId) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getParentId,parentId);
        List<Category> categories = baseMapper.selectList(queryWrapper);
        if(!ObjectUtils.isEmpty(categories)){
            for (Category category : categories) {
                //判断该分类下是否有子分类
                long count = count(new LambdaQueryWrapper<Category>().eq(Category::getParentId,category.getId()));
                category.setHasChildren(count>0);
            }
        }
        return categories;
    }

    @Override
    public List<CategoryVo> getCategoryList() {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getParentId,0);
        List<Category> categories = baseMapper.selectList(queryWrapper);
        List<CategoryVo> list = new ArrayList<>();
        for (Category category : categories) {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(category,categoryVo);
            list.add(categoryVo);
        }
        return list;
    }

    @Override
    public List<CategoryVo> tree() {
     //先查询出所有分类
        List<Category> categories = baseMapper.selectList(null);
        //将Po集合转换成Vo集合
        if(!Collections.isEmpty(categories)){
            List<CategoryVo> categoryVoList = categories.stream().map(category -> {
                CategoryVo categoryVo = new CategoryVo();
                BeanUtils.copyProperties(category, categoryVo);
                return categoryVo;
            }).collect(Collectors.toList());
            //创建一个集合来存储顶级分类
            ArrayList<CategoryVo> categoryVos = new ArrayList<>();
            //将Vo集合转化成map能快读数据
            Map<Long, CategoryVo> categoryVoMap = categoryVoList
                    .stream()
                    .collect(Collectors.toMap(CategoryVo::getId, categoryVo -> categoryVo));
            //遍历所有分类找出父节点将其存储在集合中
            for (CategoryVo categoryVo : categoryVoList) {
                if(categoryVo.getParentId()==0){
                    categoryVos.add(categoryVo);
                }else {
                    //子节点找到对应的父节点
                    Long parentId = categoryVo.getParentId();
                    categoryVoMap.get(parentId).getChildren().add(categoryVo);
                }
            }
            return  categoryVos;
        }
        return null;
    }

//    @Override
//    public List<CategoryVo> buildtree(List<CategoryVo> categoryVoList) {
//       //创建一个集合用来装顶级分类
//        ArrayList<CategoryVo> categoryVos = new ArrayList<>();
//        //将上层方法传过来的集合转化成map方便快速得到数据
//        Map<Long, CategoryVo> categoryVoMap = categoryVoList
//                .stream()
//                .collect(Collectors.toMap(CategoryVo::getId, categoryVo -> categoryVo));
//        //遍历所有分类找到父节点添加到集合中
//        for (CategoryVo categoryVo : categoryVoList) {
//            //判断是否是父节点然后添加到集合中
//            if(categoryVo.getParentId()==0){
//                categoryVos.add(categoryVo);
//            }else {
//                //如果不是父节点就找对应的父节点添加进去
//                Long parentId = categoryVo.getParentId();
//                List<CategoryVo> children = categoryVoMap.get(parentId).getChildren();
//                children.add(categoryVo);
//            }
//        }
//        return categoryVos;
//    }
}
